package com.cn.exception;

import com.cn.xc.utils.StringUtil;

/**
 * FUN: 自定义异常
 *
 * @author sushy
 * @date 2016-3-23 
 */
public class MyException extends RuntimeException{
	private static final long serialVersionUID = -8427058861461828047L;
	
	private Object obj;//# 异常
	private String expDesc;//# 异常描述

	public MyException(Object obj) {
		this(obj, (String[]) null);
	}

	public MyException(Object expCode, String... args) {
		this(expCode, null, args);
	}

	public MyException(Object obj, Throwable cause, String... args) {
		this.setObj(obj);
		this.expDesc = getErrorMsg();
		if (cause != null) {
			super.initCause(cause);
		}
	}

	public String getExpDesc() {
		return expDesc;
	}

	public String getMessage() {
		return StringUtil.isNotEmpty(expDesc) ? expDesc : obj.toString();
	}

	/**
	 * 获取详细异常信息
	 * @return
	 */
	public String getErrorMsg() {
		StringBuffer sb = new StringBuffer(this.getMessage());
		Throwable cause = this.getCause();
		if (cause != null && !(cause instanceof MyException)) {
			sb.append("\n原始异常产生原因:");
			StackTraceElement[] es = cause.getStackTrace();
			for (int i = 0; i < es.length; i++) {
				sb.append(es[i] + "\n");
			}
		}
		return sb.toString();
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public Object getObj() {
		return obj;
	}


}
